package br.ucb.gafs.jimagefun.test;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import br.ucb.gafs.jimagefun.FunPic;
import br.ucb.gafs.jimagefun.Gender;
import br.ucb.gafs.jimagefun.Position;
import br.ucb.gafs.jimagefun.facedetector.FaceDetector;

public class Main {
	private static BufferedImage getImage(String image) throws IOException{
		return ImageIO.read(Main.class.getClassLoader().getResourceAsStream(image));
	}
	
	public static void main(String[] args) throws IOException {
		BufferedImage image;
		// ANA
		image = getImage("Imagem ANA.jpg");

//		// ALISON
//		image = getImage("Imagem ALISON.jpg");
		
//		// TEST
		image = getImage("test.jpg");

//		// DEL
//		image = getImage("Imagem DEL.jpg");
		
//		// MSN
//		image = getImage("imagem_padrao.png");
		
//		// Default User
//		image = getImage("imagem_default_user.PNG");

		List<Rectangle> faces = FaceDetector.findFaces(image, 5, 40);
		
		FunPic funPic;
		
		funPic = new FunPic(getImage("natal.png"), -0.15, -0.35, 1.17, 1.3, Position.FACE, Gender.BOTH);
//		funPic = new FunPic(getImage("pascoa.png"), -0.3, -0.65, 1.65, 1.9, Position.FACE, Gender.BOTH);
//		funPic = new FunPic(getImage("carnaval6.png"), -0.45, -0.85, 1.85, 2.0, Position.FACE, Gender.FEMALE);
		
		paint(funPic, image, faces);
		
		show(image);
		
		saveImage(image, new FileOutputStream("saida.png"));
	}

	private static void paint(FunPic funPic, Image image, List<Rectangle> faces) throws IOException {
		funPic.drawIn(image, faces);
	}

	private static void saveImage(Image image, OutputStream output) throws IOException {
		BufferedImage buff = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
		buff.getGraphics().drawImage(image, 0, 0, null);
		ImageIO.write(buff, "PNG", output);
	}

	private static void show(Image image) {
		JFrame jframe = new JFrame();
		jframe.setSize(800, 600);
		jframe.setLocationRelativeTo(null);
		jframe.setLayout(new BorderLayout());

		jframe.add(BorderLayout.CENTER, new JLabel(new ImageIcon(image)));
		
		jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jframe.setVisible(true);
	}
}
